package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Lff
 * @date 2022/3/1
 */
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name) {
        Page<Dish> pages = dishService.findAllByPage(page, pageSize, name);
        Page<DishDto> dtoPage = new Page<>();
        BeanUtils.copyProperties(pages, dtoPage, "records");
        List<DishDto> dishDtos = pages.getRecords().stream().map(dish -> {
            String categoryName = categoryMapper.selectById(dish.getCategoryId()).getName();
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);
            dishDto.setCategoryName(categoryName);
            return dishDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(dishDtos);
        return R.success(dtoPage);
    }


    @PostMapping
    public R<String> addDish(@RequestBody DishDto dishDto) {
        dishService.saveDish(dishDto);
        return R.success("添加成功！");
    }

    /**
     * 根据id查询菜品信息，用户页面数据回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> getOne(@PathVariable Long id) {
        DishDto dishDto = dishService.findById(id);
        return R.success(dishDto);
    }

    /**
     * 执行菜品数据修改
     *
     * @param dishDto
     * @return
     */
    @PutMapping
    @CacheEvict(value = "dishCache", allEntries = true)
    public R<String> updateDish(@RequestBody DishDto dishDto) {
        dishService.updateWithFlavors(dishDto);
        return R.success("修改成功！");
    }

    /**
     * 删除商品
     *
     * @param ids
     * @return
     */
    @DeleteMapping()
    @CacheEvict(value = "dishCache", allEntries = true)
    public R<String> delByIds(Long[] ids) {
        dishService.delByIds(ids);
        return R.success("删除成功！");
    }

    /**
     * 修改商品状态
     */
    @PostMapping("/status/{status}")
    @CacheEvict(value = "dishCache", allEntries = true)
    public R<String> upStatus(@PathVariable Integer status, Long[] ids) {
        dishService.updateDishStatus(status, ids);
        return R.success("修改成功！");
    }

    /**
     * 获取单个分类下的所有菜品
     * 用于添加套餐时，选择指定分类菜品添加到套餐中
     * C端用户登录成功之后首页数据显示
     */
    @GetMapping("/list")
    @Cacheable(value = "dishCache", key = "'dish_'+ #dish.categoryId + '_cache'")
    public R<List<DishDto>> list(Dish dish) {
        List<DishDto> lists = dishService.getListByCategoryId(dish);
        return R.success(lists);
    }

}
